package com.bcd.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.google.common.collect.Collections2;
import com.mysql.jdbc.Driver;
import com.usth.model.FlightVO;

public class DBUtils {
	
	public static Connection getconnection(){
		Connection con=null;
		try {
			ResourceBundle rbd=ResourceBundle.getBundle("jdbc");
			Class.forName(rbd.getString("sql.driverClassName"));
			con=DriverManager.getConnection(rbd.getString("sql.url"),rbd.getString("sql.username"),rbd.getString("sql.password"));
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return con;
	}
	public static void close(Connection con){
		try {
			if(!con.isClosed()){
				con.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		
	}
	public static void page(int pnum,String sessionname,String sname){
		HttpSession session=ServletActionContext.getRequest().getSession();
		List<FlightVO> list=(List<FlightVO>) session.getAttribute(sessionname);
		int i=0;
		if(list!=null){
			i=list.size();
		}
		
		if(i<8*pnum&&i!=0){
			list=list.subList(8*(pnum-1), i);
		}else if(i==0){
			list=null;
		}
		else{
			list=list.subList(8*(pnum-1), 8);
		}
		session.setAttribute(sname, list);
//		System.out.println(list.size());
	}
//	public static void main(String[] args) {
//		DBUtils.getconnection();
//	}
}
